package com.zyd.module.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zyd.common.util.Pagination;
import com.zyd.common.util.R;
import com.zyd.enums.GlobalConstant;
import com.zyd.module.admin.model.ProductImage;
import com.zyd.module.admin.model.ProductInfos;
import com.zyd.module.admin.model.ProductType;
import com.zyd.module.admin.service.ProductImageService;
import com.zyd.module.admin.service.ProductInfosService;
import com.zyd.module.admin.service.ProductTypeService;
import com.zyd.module.admin.service.RedisService;
import com.zyd.module.admin.vo.ProductInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ranhoune
 * @description 产品页面控制器
 * @date 2022-05-01
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/index/product")
public class ProductsController {

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductInfosService productInfosService;

    @Autowired
    private ProductImageService productImageService;

    @Autowired
    private RedisService redisService;

    @GetMapping("/productsTypeList")
    public R getProductsTypeListInfo(){
        List<ProductType> productTypeList = productTypeService.list();

        return R.ok().data("productTypeList",productTypeList);
    }

    @PostMapping("/productsInfoList")
    public R getProductsInfoListInfo(@RequestBody Map map){
        System.out.println(map);
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        ProductInfoVo infoVo = new ProductInfoVo();

        IPage<ProductInfos> page = new Pagination<>(pageNumber, pageSize);
        List<ProductInfos> productInfosList = productInfosService.findProductPages(page,infoVo);
        long total = page.getTotal();
        map.clear();
        map.put("productInfosList",productInfosList);
        map.put("totalPage",page.getTotal());
        return R.ok().data("productMap",map);
        // return R.ok();
    }

    @PostMapping("/productsInfoListByType")
    public R getProductsInfoListByType(@RequestBody Map map){
        Integer typeId = Integer.parseInt((String) map.get("typeId"));
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        IPage<ProductInfos> page = new Pagination<>(pageNumber, pageSize);
        List<ProductInfos> productsInfoListByType = productInfosService.getProductsInfoListByType(typeId,page);
        map.clear();
        map.put("productsInfoList",productsInfoListByType);
        map.put("totalPage",page.getTotal());
        return R.ok().data("productMap",map);
    }

    @PostMapping("/getProductById")
    public R getProductById(@RequestBody Map map){
        String productId = (String) map.get("productId");
        ProductInfos productInfos = productInfosService.getAndThumbnailById(productId);
        List<ProductImage> productImages2 = productImageService.selectByProIdAndImgTypeIdList(productId, GlobalConstant.PRODUCT_SHOW);
        List<ProductImage> productImages3 = productImageService.selectByProIdAndImgTypeIdList(productId, GlobalConstant.PRODUCT_DETAIL);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("线程执行记录产品访问数量");
                ProductInfos other=new ProductInfos();
                other.setId(productId);
                other.setLookCount((1+productInfos.getLookCount()));
                productInfosService.updateById(other);
            }
        });
        thread.start();
        map.clear();
        map.put("productInfos",productInfos);
        map.put("productImage2",productImages2);
        map.put("productImage3",productImages3);
        return R.ok().data("productMap",map);
    }
//    //测试用
//    @GetMapping("/getProductById/{productId}")
//    public R geProductById(@PathVariable String productId){
//        ProductInfos productInfos = productInfosService.getAndThumbnailById(productId);
//        List<ProductImage> productImages2 = productImageService.selectByProIdAndImgTypeIdList(productId, GlobalConstant.PRODUCT_SHOW);
//        List<ProductImage> productImages3 = productImageService.selectByProIdAndImgTypeIdList(productId, GlobalConstant.PRODUCT_DETAIL);
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                log.info("线程执行记录产品访问数量");
//                ProductInfos other=new ProductInfos();
//                other.setId(productId);
//                other.setLookCount((1+productInfos.getLookCount()));
//                productInfosService.updateById(other);
//            }
//        });
//        thread.start();
//        Map<String, Object> map=new HashMap<>();
//        map.clear();
//        map.put("productInfos",productInfos);
//        map.put("productImage2",productImages2);
//        map.put("productImage3",productImages3);
//        return R.ok().data("productMap",map);
//    }
}
